#include "projectmanager.h"
#include <QDomDocument>
#include <QFile>

ProjectManager::ProjectManager() {}

ProjectManager::ProjectManager(QTreeWidget *pTreeWidget)
{
    m_pTreeWidgetProject = pTreeWidget;
}

void ProjectManager::Init()
{
    //ui->treeWidgetProject->setColumnCount(2);
    //ui->treeWidgetProject->setHeaderLabels(QStringList() << "Name" << "Value");
    m_pTreeWidgetProject->header()->hide();

    QTreeWidgetItem *rootItem = new QTreeWidgetItem(QStringList() << "项目名称");
    rootItem->setIcon(0, QIcon(":/images/res/icon/Project/bearing.png"));
    m_pTreeWidgetProject->addTopLevelItem(rootItem);

    QTreeWidgetItem *configItem = new QTreeWidgetItem(QStringList() << "配置文件");
    configItem->setIcon(0, QIcon(":/images/res/icon/Project/config.png"));
    rootItem->addChild(configItem);

    QTreeWidgetItem *flowchartsItem = new QTreeWidgetItem(QStringList() << "流程文件");
    flowchartsItem->setIcon(0, QIcon(":/images/res/icon/Project/folder-close.png"));
    rootItem->addChild(flowchartsItem);

    QTreeWidgetItem *flowItem = new QTreeWidgetItem(QStringList() << "寿命预测流程文件");
    flowItem->setIcon(0, QIcon(":/images/res/icon/Project/flowchart.png"));
    flowchartsItem->addChild(flowItem);

    QTreeWidgetItem *resultsItem = new QTreeWidgetItem(QStringList() << "结果文件");
    resultsItem->setIcon(0, QIcon(":/images/res/icon/Project/folder-close.png"));
    rootItem->addChild(resultsItem);

    QTreeWidgetItem *dataItem = new QTreeWidgetItem(QStringList() << "数据文件");
    dataItem->setIcon(0, QIcon(":/images/res/icon/Project/table.png"));
    resultsItem->addChild(dataItem);

    QTreeWidgetItem *chartItem = new QTreeWidgetItem(QStringList() << "曲线文件");
    chartItem->setIcon(0, QIcon(":/images/res/icon/Project/chart.png"));
    resultsItem->addChild(chartItem);

    QTreeWidgetItem *fileItem = new QTreeWidgetItem(QStringList() << "文件");
    fileItem->setIcon(0, QIcon(":/images/res/icon/Project/file.png"));
    resultsItem->addChild(fileItem);

    m_pTreeWidgetProject->expandItem(rootItem);
}

void ProjectManager::ReadXMLProjectFile(QString filename)
{
    QDomDocument doc;

    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly)) {
        return;
    }

    if (!doc.setContent(&file)) {
        file.close();
        return;
    }
    file.close();
    // 项目根节点
    QDomElement projectElement = doc.documentElement();
    m_pTreeWidgetProject->header()->hide();
    QTreeWidgetItem *rootItem = new QTreeWidgetItem(
        QStringList() << projectElement.attribute("name", "项目文件"));
    rootItem->setIcon(0, QIcon(":/images/res/icon/Project/bearing.png"));
    m_pTreeWidgetProject->addTopLevelItem(rootItem);

    QDomNodeList configurationNodes = projectElement.elementsByTagName("Configuration");
    if (configurationNodes.count() > 0) {
        QDomNamedNodeMap map = configurationNodes.at(0).attributes();
        QDomNode node = map.namedItem("name");

        QTreeWidgetItem *configurationItem = new QTreeWidgetItem(QStringList() << node.nodeValue());
        node = map.namedItem("icon");
        configurationItem->setIcon(0,
                                   QIcon(":/images/res/icon/Project/" + node.nodeValue() + ".png"));
        rootItem->addChild(configurationItem);
    }

    QDomNodeList flowChartNodes = projectElement.elementsByTagName("FlowChart");
    if (flowChartNodes.count() > 0) {
        QTreeWidgetItem *flowChartsItem = new QTreeWidgetItem(QStringList() << "流程文件");
        flowChartsItem->setIcon(0, QIcon(":/images/res/icon/Project/folder-close.png"));
        rootItem->addChild(flowChartsItem);

        for (int i = 0; i < flowChartNodes.count(); i++) {
            QDomNamedNodeMap map = flowChartNodes.at(i).attributes();
            QDomNode node = map.namedItem("name");

            QTreeWidgetItem *flowChartItem = new QTreeWidgetItem(QStringList() << node.nodeValue());

            node = map.namedItem("icon");
            flowChartItem->setIcon(0,
                                   QIcon(":/images/res/icon/Project/" + node.nodeValue() + ".png"));
            flowChartsItem->addChild(flowChartItem);
        }
    }

    QDomNodeList resultNodes = projectElement.elementsByTagName("Result");
    if (resultNodes.count() > 0) {
        QTreeWidgetItem *resultsItem = new QTreeWidgetItem(QStringList() << "结果文件");
        resultsItem->setIcon(0, QIcon(":/images/res/icon/Project/folder-close.png"));
        rootItem->addChild(resultsItem);

        for (int i = 0; i < resultNodes.count(); i++) {
            QDomNamedNodeMap map = resultNodes.at(i).attributes();
            QDomNode node = map.namedItem("name");

            QTreeWidgetItem *resultItem = new QTreeWidgetItem(QStringList() << node.nodeValue());

            node = map.namedItem("icon");
            resultItem->setIcon(0, QIcon(":/images/res/icon/Project/" + node.nodeValue() + ".png"));
            resultsItem->addChild(resultItem);
        }
    }

    m_pTreeWidgetProject->expandItem(rootItem);
}

void ProjectManager::WriteXMLProjectFile(QString filename)
{
    QFile file(filename); //相对路径、绝对路径、资源路径都可以
    if (!file.open(QFile::WriteOnly | QFile::Truncate)) //可以用QIODevice，Truncate表示清空原来的内容
        return;
    QDomDocument doc;
    QDomProcessingInstruction instruction; //添加xml说明
    instruction = doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
    doc.appendChild(instruction);
    QDomElement root = doc.createElement("bookstore");   //创建根元素
    doc.appendChild(root);                               //添加根元素
    QDomElement bookElement = doc.createElement("book"); //一级子元素
    bookElement.setAttribute("category",
                             "CHILDREN"); //方式一：创建属性  其中键值对的值可以是各种类型
    //    QDomAttr category=doc.createAttribute("time"); //方式二：创建属性 值必须是字符串
    //    category.setValue("CHILDREN");
    //    bookElement.setAttributeNode(time);
    QDomElement titleElement = doc.createElement("title");        //二级子元素
    titleElement.appendChild(doc.createTextNode("Harry Potter")); //为titleElement元素增加文本
    QDomElement yearElement = doc.createElement("year");          //二级子元素
    yearElement.appendChild(doc.createTextNode("2005"));          //为yearElement元素增加文本
    bookElement.appendChild(titleElement);                        //增加子元素
    bookElement.appendChild(yearElement);
    root.appendChild(bookElement);
    QTextStream out_stream(&file); //输出到文件
    doc.save(out_stream, 2);       //缩进4格
    file.close();
}

void ProjectManager::CreateEmpytProjectFile(QString filename)
{
    WriteXMLProjectFile(filename);
}
